package com.justenholter.text.effects {
	
	import com.justenholter.text.effects.IEffect;
	
	/**
	 * Writes out the new text.
	 * 
	 * @author justen holter
	 */
	public class Write implements IEffect {
		
		private var _speed:Number;
		
		/**
		 * Creates a new instance of Write.
		 * 
		 * @param	speed	The amount of characters to write per call to <code>init()</code> or <code>next()</code>.
		 */
		public function Write(speed:int = 1) {
			_speed = speed;
		}
		
		/**
		 * @inheritDoc
		 */
		public function init(curText:String, newText:String):String {
			return next("", newText);
		}
		
		/**
		 * @inheritDoc
		 */
		public function next(curText:String, newText:String):String {
			
			var count:Number = 0;
			var temp:String = "";
			
			for(var i:Number = 0; i < newText.length; i++){
				
				var curChar:String = curText.charAt(i);
				var newChar:String = newText.charAt(i);
				
				temp += newChar;
				
				if(curChar != newChar){
					if(++count == _speed || i == (newText.length - 1)) {
						return temp;
					}
				}
			}
			
			return temp;
		}
	}
}